package cn.yunyichina.provider.base.service.impl;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.yunyichina.provider.iface.entity.ErrorCodeDefine;

/**
 * 旧数据库同步到新数据库
 * @author adriftor
 *
 */
@Service("syncdataServiceImpl")
public class SyncDataServiceImpl {
	
	private static final Logger logger = LoggerFactory.getLogger(SyncDataServiceImpl.class);
	
	@Resource
	UserSyncServiceImpl userSyncServiceImpl;
	
	@Resource
	UserOpenidSyncServiceImpl userOpenidSyncServiceImpl;
	
	@Resource
	UserRelationSyncServiceImpl userRelationSyncServiceImpl;
	
	@Resource
	UserFamilySyncServiceImpl userFamilySyncServiceImpl;
	
	@Resource
	MedicalCardSyncServiceImpl medicalCardSyncServiceImpl;
	
	@Resource
	OrderSyncServiceImpl orderSyncServiceImpl;
	
	private static boolean isRunning = false;
	
	/**
	 * yyt.base.syncdata.do
	 */
	public void doSyncData() {
		if (isRunning) {
			logger.warn("数据同步正在运行，禁止重入");
			return;
		}
		
		isRunning = true;
		
		try {
			userSyncServiceImpl.doUserSync();
			userOpenidSyncServiceImpl.doUserOpenidSync();
			userRelationSyncServiceImpl.doUserRelationSync();
			medicalCardSyncServiceImpl.doMedicalCardSync();
			
			orderSyncServiceImpl.doOrderSync();
		}
		catch (Exception ex) {
			logger.error("数据同步发生异常:"+ErrorCodeDefine.getStackTrace(ex));
		}
		finally {
			isRunning = false;
		}
		
	}
	
}
